package com.sg.business.model.sap;

import java.util.Map;
import java.util.Set;

import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.IRepository;
import com.sap.mw.jco.JCO;
import com.sap.mw.jco.JCO.Client;
import com.sap.mw.jco.JCO.Function;
import com.sap.mw.jco.JCO.Structure;

public class JCO_BAPI_INTERNALORDER_CREATE {

	private static final String REPOSITORY_NAME = "MYRepository"; //$NON-NLS-1$

	private static final String FUNCTION_NAME = "ZXFUN_INTERNALORDER_CREATE"; //$NON-NLS-1$

	public void createWorkOrder(Map<String, Object> parameter) throws Exception {
		Client client = SapJCOToolkit.getSAPClient();
		IRepository repository = JCO.createRepository(REPOSITORY_NAME, client);

		IFunctionTemplate ftemplate = repository
				.getFunctionTemplate(FUNCTION_NAME);

		if (ftemplate == null) {
			throw new IllegalArgumentException(
					"Can not get function template, Name:" + FUNCTION_NAME); //$NON-NLS-1$
		}

		Function function = ftemplate.getFunction();
		Structure inputStr = function.getImportParameterList().getStructure(
				"INPUT_DATA");
		Set<String> keySet = parameter.keySet();
		for (String key : keySet) {
			inputStr.setValue(parameter.get(key), key);
		}
		client.execute(function);

		// order ID
		// String outputOrderID =
		// function.getExportParameterList().getString("ORDERID");
		// System.out.println("outputOrderID:" + outputOrderID);

		// message
		// String message =
		// function.getExportParameterList().getString("MESSAGE");
		// System.out.println("message:" + message);
	}

}
